<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">信息管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="dialogVisible=true">+ 新建人群</el-button>
  </div>
  <div class="page-container">
    <!-- 查询表单 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="人群名称：">
              <el-input
                  v-model="queryParams.name"
                  placeholder="请输入人群名称"
                  clearable
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="发布状态：">
              <el-select
                  v-model="queryParams.status"
                  placeholder="选择发布状态"
                  clearable
              >
                <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="更新时间：">
              <el-date-picker
                  v-model="queryParams.dateRange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24" style="text-align: right;">
            <el-button @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleQuery">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 人群列表 -->
    <el-card class="table-card">
      <el-table
          :data="tableData"
          style="width: 100%"
          v-loading="loading"
          empty-text="暂无数据"
          border
      >
        <el-table-column prop="id" label="编号" width="80" align="center" />
        <el-table-column prop="name" label="人群名称" min-width="120" />
        <el-table-column prop="userCount" label="用户数量" width="120" align="center" />
        <el-table-column prop="activityCount" label="活动数量" width="120" align="center" />
        <el-table-column prop="description" label="描述" min-width="180" show-overflow-tooltip />
        <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
        <el-table-column prop="status" label="启动状态" width="120" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === '1' ? 'success' : 'danger'">
              {{ row.status === '1' ? '已启动' : '未启动' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button
                size="small"
                :type="row.status === '1' ? 'danger' : 'success'"
                @click="toggleStatus(row)"
            >
              {{ row.status === '1' ? '停止' : '启动' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            :total="total"
            :page-sizes="[10, 20, 30, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新建人群弹窗 -->
    <el-dialog
        v-model="dialogVisible"
        title="新建人群"
        :width="`50%`"
        style="margin-top: 5vh !important;"
    >
      <el-form :model="newUserGroupForm" label-width="100px">
        <el-form-item label="人群名称">
          <el-input
              v-model="newUserGroupForm.name"
              placeholder="请输入人群名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户数量">
          <el-input-number
              v-model="newUserGroupForm.userCount"
              :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="活动数量">
          <el-input-number
              v-model="newUserGroupForm.activityCount"
              :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
              v-model="newUserGroupForm.description"
              type="textarea"
              placeholder="请输入描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="启动状态">
          <el-switch v-model="newUserGroupForm.status"></el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <span style="display: flex; justify-content: flex-end; gap: 10px;">
          <el-button @click="dialogVisible=true">取消</el-button>
          <el-button type="primary" @click="handleCreate">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 查询参数
const queryParams = reactive({
  name: '',
  status: '',
  dateRange: [],
  pageNum: 1,
  pageSize: 10
})

// 表格数据
const tableData = ref([])
const total = ref(0)
const loading = ref(false)

// 状态选项
const statusOptions = ref([
  { value: '1', label: '已启动' },
  { value: '0', label: '未启动' }
])

// 获取人群列表
const getList = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    tableData.value = [
      {
        id: '1',
        name: '高价值客户',
        userCount: 1250,
        activityCount: 8,
        description: '近3个月消费超过5000元的客户群体',
        createTime: '2023-05-10 14:30:22',
        status: '1'
      },
      {
        id: '2',
        name: '潜在客户',
        userCount: 3560,
        activityCount: 5,
        description: '浏览过商品但未下单的客户群体',
        createTime: '2023-06-15 09:12:45',
        status: '0'
      },
      {
        id: '3',
        name: '新注册用户',
        userCount: 890,
        activityCount: 3,
        description: '最近30天内注册的新用户',
        createTime: '2023-07-01 16:45:18',
        status: '1'
      }
    ]
    total.value = 3
    loading.value = false
  }, 500)
}

// 查询按钮
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮
const handleReset = () => {
  Object.assign(queryParams, {
    name: '',
    status: '',
    dateRange: [],
    pageNum: 1,
    pageSize: 10
  })
  getList()
}

// 分页大小变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  getList()
}

// 当前页变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  getList()
}

// 编辑操作
const handleEdit = (row) => {
  ElMessage.info(`编辑人群：${row.name}`)
  // 这里应该是打开编辑对话框的逻辑
  dialogVisible.value = true
}

// 切换状态
const toggleStatus = (row) => {
  const newStatus = row.status === '1' ? '0' : '1'
  // 这里应该是调用API更新状态
  row.status = newStatus
  ElMessage.success(`已${newStatus === '1' ? '启动' : '停止'}人群 ${row.name}`)
}

// 初始化加载数据
onMounted(() => {
  getList()
})

// 新建人群弹窗相关数据
const dialogVisible = ref(false)

const newUserGroupForm = reactive({
  name: '',
  userCount: 0,
  activityCount: 0,
  description: '',
  status: false
})
// 新建人群操作
const handleCreate = () => {
  // 这里应该是调用API创建新人群
  const newGroup = {
    id: String(tableData.value.length + 1),
    ...newUserGroupForm,
    status: newUserGroupForm.status ? '1' : '0',
    createTime: new Date().toLocaleString()
  }
  tableData.value.push(newGroup)
  total.value++
  dialogVisible.value = false
  ElMessage.success('新建人群成功')
}
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
}

.page-title {
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: bold;
}

.filter-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>